﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InspectionVehicule.Controleur;
using InspectionVehicule.WorkflowHost;
using InspectionPageFlow;

namespace InspectionVehicule.Pages
{
    public abstract class PageBase : Page
    {

        protected override void OnLoad(EventArgs e)
        {
            State = Session["PageState"];

            if (Session["WorkflowId"] != null)
                WorkflowId = (Guid)Session["WorkflowId"];

            base.OnLoad(e);
        }

        protected override bool OnBubbleEvent(object source, EventArgs args)
        {
            IButtonControl bouton = source as IButtonControl;

            if (bouton != null && !String.IsNullOrEmpty(bouton.CommandName))
            {
                InspectionWorkflowHost host = new InspectionWorkflowHost();
                host.WorkflowId = WorkflowId;
                
                
                string urlPage = host.RouterPage(State, EtapeEnCours, (NavigationAction)Enum.Parse(typeof(NavigationAction), bouton.CommandName));
                Session["PageState"] = host.Donnees;
                Response.Redirect(urlPage);
            }

            return base.OnBubbleEvent(source, args);
        }

        public abstract object State { get; set; }
        protected abstract string EtapeEnCours {get ;}

        internal Guid WorkflowId
        {
            get
            {
                return (Guid)ViewState["WorkflowId"];
            }
            set
            {
                ViewState["WorkflowId"] = value;
            }
        }

        //public abstract string Nom { get; }
        public string Name
        {
            get { return this.GetType().BaseType.Name; }
        } 
       
    }
}